package com.apac.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import javax.persistence.Tuple;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Root;

import com.apac.bom.AbstractBOM;
import com.apac.bom.JmesaTableColumn;

public class JmesaColumnsDAO extends AbstractDAO {

	@Override
	public AbstractBOM findByKey(String id) {
		// TODO Auto-generated method stub
		int idx = 0;
		try {
			idx = Integer.valueOf(id);
			return this.findByKey(idx);
		} catch (NumberFormatException e) {
			return null;
		}
	}

	public JmesaTableColumn findByKey(int id) {
		JmesaTableColumn column = null;
		column = em.find(JmesaTableColumn.class, id);
		return column;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<AbstractBOM> findAll() {
		// TODO Auto-generated method stub
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<JmesaTableColumn> cq = cb
				.createQuery(JmesaTableColumn.class);
		Root<JmesaTableColumn> root = cq.from(JmesaTableColumn.class);
		cq.select(root);
		TypedQuery tq = em.createQuery(cq);
		List<AbstractBOM> columns = tq.getResultList();
		return columns;
	}

	@Override
	public boolean update(AbstractBOM entity) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean delete(AbstractBOM entity) {
		// TODO Auto-generated method stub
		return false;
	}

	public List<String> findByTableName(String tbl) {
		List<String> colNames = new ArrayList<String>();
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<String> cq = cb.createQuery(String.class);
		Root<JmesaTableColumn> root = cq.from(JmesaTableColumn.class);
		ParameterExpression<String> param = cb.parameter(String.class,
				"tblName");
		cq.multiselect(root.get("jmesaColName")).where(
				cb.equal(root.get("jmesaTable").<String> get("jmesaTblName"),
						param));
		TypedQuery<String> tq = em.createQuery(cq);
		tq.setParameter("tblName", tbl);
		colNames = tq.getResultList();
		return colNames;
	}

	public List<String> findByTableId(int tbl) {
		List<String> colNames = new ArrayList<String>();
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<String> cq = cb.createQuery(String.class);
		Root<JmesaTableColumn> root = cq.from(JmesaTableColumn.class);
		ParameterExpression<Integer> param = cb.parameter(Integer.class,
				"tblId");
		cq.multiselect(root.get("jmesaColName")).where(
				cb.equal(root.get("jmesaTable").<Integer> get("jmesaTblCode"),
						param));
		TypedQuery<String> tq = em.createQuery(cq);
		tq.setParameter("tblId", tbl);
		colNames = tq.getResultList();
		return colNames;
	}

	public TreeMap<String, String> findMethodByTableId(int tbl) {
		List<Tuple> colNames = new ArrayList<Tuple>();
		TreeMap<String, String> methods = new TreeMap<String, String>();
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Tuple> cq = cb.createQuery(Tuple.class);
		Root<JmesaTableColumn> root = cq.from(JmesaTableColumn.class);
		ParameterExpression<Integer> param = cb.parameter(Integer.class,
				"tblId");
		cq.multiselect(root.get("jmesaColName"), root.get("jmesaMthdName"))
				.where(cb.equal(
						root.get("jmesaTable").<Integer> get("jmesaTblCode"),
						param));
		TypedQuery<Tuple> tq = em.createQuery(cq);
		tq.setParameter("tblId", tbl);
		
		colNames = tq.getResultList();
		
		for (Tuple t : colNames) {
//			System.out.println(t);
			String key = (String) t.get(0);
//			System.out.println(key);
			String method = (String) t.get(1);
//			System.out.println(method);

			methods.put(key, method);
		}
		return methods;
	}

	public List<String> findByMethod(String tbl) {
		List<String> colNames = new ArrayList<String>();
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<String> cq = cb.createQuery(String.class);
		Root<JmesaTableColumn> root = cq.from(JmesaTableColumn.class);
		ParameterExpression<String> param = cb.parameter(String.class,
				"tblName");
		cq.multiselect(root.get("jmesaColName")).where(
				cb.equal(root.get("jmesaTable").<String> get("jmesaTblName"),
						param));
		TypedQuery<String> tq = em.createQuery(cq);
		tq.setParameter("tblName", tbl);
		colNames = tq.getResultList();
		return colNames;
	}
}
